package com.daidai.activitys

import android.animation.ValueAnimator
import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.View
import androidx.core.view.marginBottom
import androidx.core.view.marginLeft
import androidx.core.view.marginRight
import androidx.core.view.marginTop
import com.daidai.base.BaseActivity
import com.daidai.spinner.R
import com.daidai.tools.G
import kotlin.math.max
import kotlin.math.min

class XView @JvmOverloads constructor(
        ctx: Context,
        attrs: AttributeSet? = null,
        defStyle: Int = 0
) : View(ctx, attrs, defStyle) {
    val beiziBitmap: Bitmap
    val shuiBitmap: Bitmap

    val paint : Paint
    var drawHeight = 0f

    val imgWidth:Int
    val imgHeight:Int

    init {
        beiziBitmap = BitmapFactory.decodeResource(resources, R.mipmap.dd_heshui_beizi)
        shuiBitmap = BitmapFactory.decodeResource(resources, R.mipmap.red_beizi_shuih)
        G.elog("beizi==> width:${beiziBitmap.width},height:${beiziBitmap.height}")
        G.elog("shuiBitmap==> width:${shuiBitmap.width},height:${shuiBitmap.height}")
        paint = Paint(Paint.ANTI_ALIAS_FLAG)

        imgWidth = beiziBitmap.width
        imgHeight = beiziBitmap.height
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)

        var newWidth = 0
        var newHeight = 0

        // width
        val widthMode = MeasureSpec.getMode(widthMeasureSpec)
        if (widthMode == MeasureSpec.EXACTLY){
            newWidth = MeasureSpec.getSize(widthMeasureSpec)
        }else{
            newWidth = imgWidth
            if (widthMode == MeasureSpec.AT_MOST){
                newWidth = min(MeasureSpec.getSize(widthMeasureSpec),newWidth)
            }
        }

        // height
        val heightMode = MeasureSpec.getMode(heightMeasureSpec)
        if (heightMode == MeasureSpec.EXACTLY){
            newHeight = MeasureSpec.getSize(heightMeasureSpec)
        }else{
            newHeight = imgHeight
            if (heightMode == MeasureSpec.AT_MOST){
                newHeight = min(MeasureSpec.getSize(heightMeasureSpec),newHeight)
            }
        }
        G.elog("newWidth:$newWidth,newHeight:$newHeight")
        setMeasuredDimension(newWidth,newHeight)
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)

        canvas?.let {

            //G.elog("x:$x,y:$y,width:$width,height:$height")
            //G.elog("padding left:$paddingLeft,top:$paddingTop,right:$paddingRight,bottom:$paddingBottom")
            //G.elog("margin left:$marginLeft,top:$marginTop,right:$marginRight,bottom:$marginBottom")

            val top = y + height - drawHeight
            val left = x
            val right = left + width
            val bottom = y + height

            val rect = Rect(0,0,width,height)

            // Rect(0,0,beiziBitmap.width,beiziBitmap.height)
            it.drawBitmap(beiziBitmap, null ,rect,null)
            it.save()
            // it.clipRect(RectF(x,y + height - drawHeight,x + width,y + height))
            it.clipRect(RectF(0f,height - drawHeight ,width.toFloat(),height.toFloat()))
//            it.drawColor(Color.RED)
            it.drawBitmap(shuiBitmap,null,rect,null)
            it.restore()


            // 背景设置成黑色
//            it.drawColor(Color.BLACK)

//            val saved = it.saveLayer(x,y,x+width,y+height,paint)
//
//            // 绘制
//            it.drawBitmap(beiziBitmap,x,y,paint)
//            paint.xfermode = fermode
//            it.drawBitmap(shuiBitmap,x,y,paint)
//            paint.xfermode = null
//            canvas.restoreToCount(saved)
        }
    }
}


class XperformActivity : BaseActivity() {

    lateinit var xView: XView

    override fun getContentViewResId(): Int = R.layout.activity_xperform

    override fun initView() {
        xView = findViewById(R.id.xview)
    }

    override fun initData() {

        // 来个动画
        val anim = ValueAnimator.ofFloat(0f,513f,0f)
        anim.duration = 4000
        anim.addUpdateListener {
            val value = it.animatedValue as Float
            xView.drawHeight = value
            xView.invalidate()
        }
        anim.start()
    }
}